package com.hnit.applet.common.enums;

import org.springframework.lang.Nullable;

/**
 * ResponseStatus
 * <p>
 * Created on 2020/1/4
 *
 * @author tongw
 */
public enum ResponseStatus {

    /**
     * [*] 服务器成功返回用户请求的数据
     */
    OK(200, "OK"),
    /**
     * [*]：用户发出的请求出现业务异常, 错误信息显示在页面上
     */
    INVALID_REQUEST(400, "INVALID REQUEST"),
    /**
     * [*]：表示用户没有权限（令牌、用户名、密码错误, 需要重新登录)
     */
    UNAUTHORIZED(401, "UNAUTHORIZED"),

    /**
     * [*]：流量限制
     */
    TOO_MANY_REQUESTS(429, "TOO_MANY_REQUESTS"),
    /**
     * [*]：系统异常, 需要跳转到系统异常错误页面(比如: 该页面可显示"系统未知异常, 请联系管理员"等信息)
     * 异常详细信息,需要前台注释起来或者将错误信息输出到控制台console.error(...)
     */
    INTERNAL_SERVER_ERROR(500, "INTERNAL SERVER ERROR"),

    /**
     * [*]：网关异常
     */
    BAD_GATEWAY(502, "BAD_GATEWAY");

    private final int value;
    private final String reasonPhrase;

    ResponseStatus(int value, String reasonPhrase) {
        this.value = value;
        this.reasonPhrase = reasonPhrase;
    }

    public int value() {
        return this.value;
    }

    public String getReasonPhrase() {
        return this.reasonPhrase;
    }

    public static ResponseStatus valueOf(int statusCode) {
        ResponseStatus status = resolve(statusCode);
        if (status == null) {
            throw new IllegalArgumentException("No matching constant for [" + statusCode + "]");
        } else {
            return status;
        }
    }

    @Nullable
    public static ResponseStatus resolve(int statusCode) {
        ResponseStatus[] var1 = values();
        int var2 = var1.length;

        for (int var3 = 0; var3 < var2; ++var3) {
            ResponseStatus status = var1[var3];
            if (status.value == statusCode) {
                return status;
            }
        }

        return null;
    }
}
